class Solution {
public:
    int minInsertions(string s) {
        int n = s.size();
        vector<vector<int>> dp(n, vector<int>(n, 1));

        for (int j = 1; j < n; j++)
        {
            for (int i = j - 1; i >= 0; i--)
            {
                if (s[i] == s[j])
                {
                    if (j - i == 1)
                        dp[i][j] = 2;
                    else
                        dp[i][j] = 2 + dp[i + 1][j - 1];
                }
                else
                    dp[i][j] = max(dp[i + 1][j], dp[i][j - 1]);

            }
        }

        return n - dp[0][n - 1];
    }
};
